﻿using System;
using System.Collections;
using System.Xml.Linq;
using System.Text;
using ClrExtensions.Net.Rest;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class User
    {
        #region Locals

        private string _FirstName;
        private string _LastName;
        private bool _can_instant_watch;
        private ArrayList _Preferences;
        private Connection _Connection;
        AccessToken _access_token;
        RestCalls _restCalls = new RestCalls();

        #endregion

        #region Public

        public string FirstName
        {
            get { return _FirstName; }
        }
        public string LastName
        {
            get { return _LastName; }
        }
        public bool CanInstantWatch
        {
            get { return _can_instant_watch; }
        }
        public AccessToken Token
        {
            get { return _access_token; }
        }
        public Connection NetflixConnection
        {
            get { return _Connection; }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="connection"></param>
        public User(AccessToken accessToken, Connection connection)
        {
            //--------------------------------------------------------------------------------------------
            // Store the access token and the connection to netflix so we dont have to make mulitple calls
            //--------------------------------------------------------------------------------------------
            _access_token = accessToken;
            _Connection = connection;
        }


        /// <summary>
        /// Get the user information from NetFlix
        /// </summary>
        public void GetUser()
        {
            //--------------------------------------------------------------------------------
            // Get the user information
            //--------------------------------------------------------------------------------
            try
            {
                //--------------------------------------------------------------------------------
                // Set up the Rest call
                //--------------------------------------------------------------------------------
                RestCall rest = RestCalls.GetUsers(_access_token);

                //--------------------------------------------------------------------------------
                // Get the user
                //--------------------------------------------------------------------------------
                XElement listFragment = _Connection.RequestXmlResource(rest, _access_token);

                //--------------------------------------------------------------------------------
                // Store the user information
                //--------------------------------------------------------------------------------
                _Preferences = new ArrayList();
                _LastName = listFragment.Element("last_name").Value;
                _FirstName = listFragment.Element("first_name").Value;
                _can_instant_watch = bool.Parse(listFragment.Element("can_instant_watch").Value);

                //--------------------------------------------------------------------------------
                // Get the preferred delivery format (BluRay or Dvd)
                //--------------------------------------------------------------------------------
                foreach (var item in listFragment.Descendants())
                {
                    if (item.Name.ToString() == "preferred_formats")
                    {
                        foreach (var dec in item.Descendants())
                        {
                            foreach (var attrib in dec.Attributes())
                            {
                                if (attrib.Name.ToString() == "term")
                                {
                                    _Preferences.Add(attrib.Value);
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - User:GetUser(): Error - " + ex.Message);
            }
        }
    }
}
